<?php

class CAddress extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	public function __construct(){
		
		$this->_name=R_PRE."addresses";
		
		$this->_primary="addressid";
		
		parent::__construct();
		
	}

	public function createAddress($data){
		
		$addressid=$this->insert($data);
		
		return $addressid;
		
	}
	
	public function editAddress($addressid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('addressid = ?', $addressid);
		
		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			
			return true;
			
		}
		
		
	}
	
	public function getDetail($addressid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name);
		
		$select->where('addressid = ? ',$addressid);
		
		return $db->fetchAll($select);
		
	}
	
	public function delAddresses($addressidArray){
		
		$db=$this->getAdapter();
		
		$addressidString=join(',',$addressidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE addressid IN ($addressidString)");
		
		return $result;
		
	}
	
	public function getAllAddresses($uid)
	{
		$db=$this->getAdapter();
		$select = $db->select();
		$select->from($this->_name,'*');
		$select->where(" uid = ? ",$uid);
		$select->order('addressid desc');
		return $db->fetchAll($select);
	}
	
	public function deleteAddress($id, $uid)
	{		
		$db = $this->getAdapter();		
		$where = $db->quoteInto('addressid = ?', $id);
		$where .= $db->quoteInto(' and uid = ?', $uid);
		return $this->delete($where);
	}
	
	public function updateAddress($id, $uid, $row)
	{
		$db = $this->getAdapter();		
		$where = $db->quoteInto('addressid = ?', $id);
		$where .= $db->quoteInto(' and uid = ?', $uid);
		return $this->update($row, $where);
	}
}